// pages/record/audition/audition.js
var app = getApp();
const model = require('../../../utils/model');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    status:'pause',
    id:0,
    path:'',
    title:''
  },

  play:function(){
    var me = this;
    var path = this.data.path;
    // this.setData({
    //   status:'playing'
    // });
    wx.playVoice({
      filePath: path
    });
  },

  pause:function(){
    this.setData({
      status:'pause'
    });

    wx.pauseVoice();
  },

  tapsave:function(){
    this.save(function(res){
      if (res.data.code) {
        wx.showToast({
            title: res.data.msg,
            icon: 'success',
            duration: 2000,
            success:function(){
              wx.reLaunch({
                url:'/pages/record/choose_article/index'
              });
            }
        });
      }
      
    });
  },

  save: function (callback) {
    var filePath = this.data.path;
    var aid = this.data.id;
    var me = this;
    wx.uploadFile({
      url:'https://v1.wx91go.com/index.php?m=Api&c=WxMini&a=uploadMP3',
      filePath:filePath,
      header:{
        "Content-Type":"multipart/form-data"
      },
      name:'file',
      success:function(res){
        var path = JSON.parse(res.data).data;
        model.request({
          action:'saveReading',
          method:'POST',
          data:{
            id:aid,
            mp3_url:path
          },
          header:{
            "Content-Type":"application/x-www-form-urlencoded"
          },
          success:function (res) {
            var wid = res.data.id;
            if (callback) {
              callback(res,me.data.title);
            }
          }
        });
      }
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      id:options.id,
      path:options.path,
      title:options.title,
      scrollHeight: app.globalData.winHeight - 93 - 170
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})